package com.clippick.common;

import java.io.File;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;
import android.util.DisplayMetrics;

public class GlobalValue {

	public static boolean IS_UPDATING = false;

	// keep for http response
	public static String STAR_DATA_STR = "";
	public static String UPDATE_TIME = "";

	// get info who is user
	public static boolean IS_FIRST_LOGIN_USER = false;

	/**
	 * {max=?, count=?}
	 */
	public static Map<String, String> MAP_STAR_DATA = null;

	// give some speicialities to clippick team to debug
	public static final String CLIPPICK_TEAM = "@sentencelab.com";
	public static final String DEVELOPER1 = "icemilo@icemilo.com";
	public static final String DEVELOPER2 = "magicdo7@gmail.com";

	// user info
	public static String USER_KEY = "";
	public static String USER_ID = "";

	// clips
	/* text that user clicks on ListView */
	public static String AppClip = "";

	/* the previous text from clipboard */
	public static String PrevClip = "";

	public static String AppLatestSyncTime = "";

	// 동기화 중: 새로운 클립이 생겼을 경우, 잠시 getClipBoard thread를 중단한다.
	public static boolean isOnInsertSync = false;

	// sharedPreferences Key
	public final static String KEY = "info";

	// sharedPreferences table name
	private final static String BACKGROUND_ON = "backgroundOn";
	private final static String FIRST_VALUE_SYNC = "firstValueSync";
	private final static String BASIC_DB_UPDATE = "basicDbUpdate";
	private final static String AUTO_LAUNCH = "autoLaunch";
	private final static String SHOW_NOTIFICATION = "showNotif";

	// index for screen on-off
	public static final int ACT_OFF_SERVICE_OFF = 6001; // 둘다 꺼 있는 상태
	public static final int ACT_OFF_SERVICE_ON = 6002; // BACK
	public static final int ACT_ON_SERVICE_OFF = 6003;
	public static final int ACT_ON_SERVICE_ON = 6004;

	public static String getAppClip(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(KEY,
				Context.MODE_PRIVATE);
		return savedSession.getString("AppClip", "");
	}

	public static boolean setAppClip(Context context, String value) {
		Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
				.edit();
		return editor.putString("AppClip", value).commit();
	}

	public static boolean clearAppClip(Context context) {
		Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
				.edit();
		return editor.remove("AppClip").commit();
	}

	public static boolean setUpdateTime(Context context, String clientTime) {
		Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
				.edit();
		editor.putString("updateTime", clientTime);
		return editor.commit();
	}

	public static final String getUpdateTime(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(KEY,
				Context.MODE_PRIVATE);
		return savedSession.getString("updateTime", "");
	}

	// 이미지 저장 폴더
	public static final String CLIPPICK_IMAGE_ROOT_FOLDER = Environment
			.getExternalStorageDirectory()
			+ File.separator
			+ "ClipPick"
			+ File.separator;

	// 상단 Notification Icon 띄우기
	public static boolean isShowNotification(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(KEY,
				Context.MODE_PRIVATE);
		return savedSession.getBoolean(SHOW_NOTIFICATION, false);
	}

	// 부팅 preferences
	public static boolean isAutoReboot(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(KEY,
				Context.MODE_PRIVATE);
		return savedSession.getBoolean(AUTO_LAUNCH, false);
	}

	// 화면이 켜져있는지 값 확인
	public static int getBackgroundOn(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(KEY,
				Context.MODE_PRIVATE);
		return savedSession.getInt(BACKGROUND_ON, ACT_ON_SERVICE_OFF);
	}

	// 화면이 꺼질때나 켜질때 값을 저장
	public static boolean saveBackgroundPref(Context context, int _nBackground) {
		Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
				.edit();
		editor.putInt(BACKGROUND_ON, _nBackground);
		return editor.commit();
	}

	// 서버에 저장된 최상위 값과 local database 최상위 값 비교 후 저장
	public static boolean saveFirstSyncComplete(Context context,
			boolean isFirstSyncComplete) {
		Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
				.edit();
		editor.putBoolean(FIRST_VALUE_SYNC, isFirstSyncComplete);
		return editor.commit();
	}

	// 서버에 저장된 최상위 값과 local database 최상위 값 비교
	public static boolean isFirstSyncComplete(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(KEY,
				Context.MODE_PRIVATE);
		return savedSession.getBoolean(FIRST_VALUE_SYNC, false);
	}

	public static boolean isBasicDbUpdate(Context context) {
		SharedPreferences savedSession = context.getSharedPreferences(KEY,
				Context.MODE_PRIVATE);
		return savedSession.getBoolean(BASIC_DB_UPDATE, false);
	}

	public static boolean saveBasicDbUpdate(Context context, boolean _IsKeepBox) {
		Editor editor = context.getSharedPreferences(KEY, Context.MODE_PRIVATE)
				.edit();
		editor.putBoolean(BASIC_DB_UPDATE, _IsKeepBox);
		return editor.commit();
	}
}
